//
//  CommentViewController.m
//  NBNews
//
//  Created by Ruan on 14-10-8.
//  Copyright (c) 2014年 Hero. All rights reserved.
//

#import "CollectViewController.h"
#import "DB_Collect.h"
#import "CollectDataMode.h"
#import "CollectDetailViewController.h"
#import "UIButton+NavButton.h"
@interface CollectViewController ()
@property (nonatomic, retain) NSMutableArray * collectDateArray;
@property (nonatomic, retain) DB_Collect *db;
@property (nonatomic, retain) UIButton *editbutton;

@end

@implementation CollectViewController

-(void)dealloc
{
    [_db release];
    [_collectDateArray release];
    [super dealloc];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        self.tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        
        
        // 2. 添加左侧返回按钮
        UIButton *leftButton = [UIButton addNavButtonWithNormalImageString:@"back" andHeightLightedImageString:@"back_highlighted"];
        CGSize leftButtonSzie = leftButton.currentImage.size;
        leftButton.frame = (CGRect){CGPointZero,leftButtonSzie};
        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        [leftButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * leftBarButton = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
        self.navigationItem.leftBarButtonItem = leftBarButton;
        
        
        
        
        UIButton *titlebutton = [UIButton buttonWithType:UIButtonTypeCustom];
        titlebutton.frame = CGRectMake(0, 0, 80, 30);
        //titlebutton .contentEdgeInsets = UIEdgeInsetsMake(8, 0, 0, 0);
        titlebutton .enabled = NO;
        [titlebutton  setTitle:@"收藏列表" forState:UIControlStateNormal];
        [titlebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        self.navigationItem.titleView = titlebutton ;
        
        UIButton *editbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.editbutton = editbutton;
        editbutton.frame = CGRectMake(0, 0, 36, 36);
        //editbutton .contentEdgeInsets = UIEdgeInsetsMake(8, 0, 0, 0);
        [editbutton  setTitle:@"编辑" forState:UIControlStateNormal];
        [editbutton  setTitle:@"完成" forState:UIControlStateSelected];
        [editbutton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [editbutton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
        [editbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        UIBarButtonItem *editbarbutton = [[UIBarButtonItem alloc] initWithCustomView:editbutton];
        
        self.navigationItem.rightBarButtonItem = editbarbutton;
        [editbarbutton release];

        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    
    
    
}

- (void)addCollectDate
{
    _collectDateArray  = [[NSMutableArray alloc] init];
    
    DB_Collect *db = [[DB_Collect alloc] init];
    self.db = db;
    _collectDateArray = [db selectAllCollectData];
    self.editbutton.selected = NO;
    [self.tableView setEditing:NO animated:YES];
    [self.tableView reloadData];
    [db release];
    
}



- (void)editClick:(UIButton *)btn
{
    if (!btn.selected) {
        [self.tableView setEditing:YES animated:YES];
    }else
    {
        [self.tableView setEditing:NO animated:YES];
    }
    btn.selected = !btn.selected;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return self.collectDateArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *icell = @"icell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:icell];
    
    if (cell  == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:icell] autorelease];
    }
    
    CollectDataMode *collectData = self.collectDateArray[indexPath.row];
    cell.textLabel.text = collectData.title;
    
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectDataMode *collectData = self.collectDateArray[indexPath.row];
    [self.db deleteCollectDataWithDocid:collectData.docid];
    [self.collectDateArray removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectDetailViewController *collectDetailVC = [[CollectDetailViewController alloc] init];
    CollectDataMode *collectData = self.collectDateArray[indexPath.row];
    collectDetailVC.docid = collectData.docid;
    [self.navigationController pushViewController:collectDetailVC animated:YES];
    [collectDetailVC release];

}

- (void)backClick:(UIButton *)btn
{
    
    
    
    if (_delegate && [_delegate respondsToSelector:@selector(collectViewControllerSelectRootViewFirstIndex)]) {
        [_delegate collectViewControllerSelectRootViewFirstIndex];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



@end
